using Gateway.IApplication.Cache;
using Gateway.IApplication.Cache.Input;
using Gateway.IQuery.Cache;
using Gateway.IQuery.Cache.Request;
using Gateway.IQuery.Cache.Result;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// 缓存服务
/// </summary>
public class CacheController : BaseController
{
    private readonly ILogger<CacheController> _logger;
    private readonly IRedisCache _redisCache;
    private readonly ICacheQuery _cacheQuery;
    private readonly ICacheApp _cacheApp;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="logger"></param>
    /// <param name="redisCache"></param>
    /// <param name="cacheQuery"></param>
    /// <param name="cacheApp"></param>
    public CacheController(ILogger<CacheController> logger,
        IRedisCache redisCache, ICacheQuery cacheQuery, ICacheApp cacheApp)
    {
        _logger = logger;
        _redisCache = redisCache;
        _cacheQuery = cacheQuery;
        _cacheApp = cacheApp;
    }

    /// <summary>
    /// 运行缓存-测试
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("cache")]
    public bool RunCache()
    {
        var key = "CacheTest:";
        for (int i = 0; i <= 100000; i++)
        {
            _redisCache.Add(key + i, i.ToString(), TimeSpan.FromHours(1));
        }

        return true;
    }

    /// <summary>
    /// 获取缓存的列表
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("list")]
    public async Task<List<CacheDetailResult>> GetList()
    {
        return await _cacheQuery.GetList();
    }
    /// <summary>
    /// 获取缓存的列表对应的明细列表
    /// </summary>
    /// <returns></returns>
    [HttpPost, Route("detail/list")]
    public async Task<List<CacheDetailResult>> GetDetailList(CacheDetailRequest request)
    {
        return await _cacheQuery.GetDetailList(request);
    }
    
    
    /// <summary>
    /// 获取缓存的详情
    /// </summary>
    /// <returns></returns>
    [HttpPost, Route("detail")]
    public async Task<CacheDetailResult> GetDetail(CacheDetailRequest request)
    {
        return await _cacheQuery.GetDetail(request);
    }
    /// <summary>
    /// 获取缓存的列表
    /// </summary>
    /// <returns></returns>
    [HttpPost, Route("remove")]
    public async Task<bool> Remove(RemoveCacheInput input)
    {
        return await _cacheApp.Remove(input.Value);
    }
    
    
}